﻿Public Class ClsARDownpayment
    Dim sDocEntry As String = String.Empty
    Dim oDTDistinctHeader As DataTable = Nothing
    Dim oDTDistinctLine As DataTable = Nothing

    Dim oDataViewHeader As DataView = Nothing
    Dim oDataViewLine As DataView = Nothing
    Dim oDataViewPayment As DataView = Nothing
    Dim lRetCode As Long

    Public Function AR_Downpayment(ByRef oDataSet As DataSet, ByRef oDICompany As SAPbobsCOM.Company, ByRef sErrDesc As String) As Long

        Dim sFuncName As String = String.Empty
        Dim oDownPayment As SAPbobsCOM.Documents

        Try
            sFuncName = "AR_Downpayment()"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function", sFuncName)


            oDataViewHeader = oDataSet.Tables(0).DefaultView
            oDataViewLine = oDataSet.Tables(1).DefaultView
            oDataViewPayment = oDataSet.Tables(2).DefaultView

            oDTDistinctHeader = oDataViewHeader.Table.DefaultView.ToTable(True, "F1")

            For IntHeader As Integer = 0 To oDTDistinctHeader.Rows.Count - 1

                oDownPayment = oDICompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oDownPayments)
                sDocEntry = oDTDistinctHeader.Rows(IntHeader).Item(0).ToString

                oDownPayment.CardCode = oDataViewLine.Item(IntHeader).Row(1).ToString
                oDownPayment.DocDate = oDataViewLine.Item(IntHeader).Row(2).ToString
                oDownPayment.DocDueDate = oDataViewLine.Item(IntHeader).Row(2).ToString
                oDownPayment.TaxDate = oDataViewLine.Item(IntHeader).Row(2).ToString

                oDownPayment.DownPaymentType = SAPbobsCOM.DownPaymentTypeEnum.dptInvoice
                oDownPayment.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Service


                For IntLine As Integer = 0 To oDataViewLine.Count - 1

                    oDataViewLine.RowFilter = "F1 = '" & sDocEntry & "'"

                    oDownPayment.Lines.AccountCode = oDataViewLine.Item(IntLine).Row(1).ToString
                    oDownPayment.Lines.LineTotal = oDataViewLine.Item(IntLine).Row(2).ToString
                    oDownPayment.Lines.Add()

                Next
                lRetCode = oDownPayment.Add()

                If lRetCode <> 0 Then

                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR - DocEntry : " & oDTDistinctHeader.Rows(IntHeader).Item(0).ToString, sFuncName)
                    Return RTN_ERROR
                Else
                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS - DocEntry : " & oDTDistinctHeader.Rows(IntHeader).Item(0).ToString, sFuncName)

                    oDICompany.GetNewObjectCode(sDocEntry)

                    If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Incoming Payment for DocEntry : " & sDocEntry, sFuncName)

                    Dim oIncomingPayment As SAPbobsCOM.Payments = oDICompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oIncomingPayments)

                    oIncomingPayment.DocType = SAPbobsCOM.BoRcptTypes.rCustomer
                    oIncomingPayment.CardCode = oDataViewHeader.Item(IntHeader).Row(1).ToString
                    oIncomingPayment.DocDate = oDataViewHeader.Item(IntHeader).Row(2).ToString
                    oIncomingPayment.DueDate = oDataViewHeader.Item(IntHeader).Row(2).ToString
                    oIncomingPayment.TaxDate = oDataViewHeader.Item(IntHeader).Row(2).ToString

                    oIncomingPayment.Invoices.InvoiceType = SAPbobsCOM.BoRcptInvTypes.it_DownPayment
                    oIncomingPayment.Invoices.DocEntry = sDocEntry

                    oIncomingPayment.Invoices.Add()

                    oIncomingPayment.CashAccount = oDataViewHeader.Item(IntHeader).Row(3).ToString
                    oIncomingPayment.CashSum = oDataViewHeader.Item(IntHeader).Row(4).ToString

                    lRetCode = oIncomingPayment.Add()

                    If lRetCode <> 0 Then
                        If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed Incoming Payment with ERROR - DocEntry : " & oDTDistinctHeader.Rows(IntHeader).Item(0).ToString, sFuncName)
                        Return RTN_ERROR
                    Else
                        If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed Incoming Payment with SUCCESS - DocEntry : " & oDTDistinctHeader.Rows(IntHeader).Item(0).ToString, sFuncName)
                    End If
                End If
            Next

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS", sFuncName)
            Return RTN_SUCCESS

        Catch ex As Exception
            Call WriteToLogFile(ex.Message, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR", sFuncName)
            Return RTN_ERROR
        End Try

    End Function

End Class
